package com.sojpt.boot.controller.business;

import com.sojpt.boot.controller.business.dao.LkyScoreTableMapper;
import com.sojpt.boot.controller.business.services.ResourceService;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.model.Item;
import com.sojpt.model.LkyTableStorage;
import com.sojpt.model.bo.LkyEvaluationVO;
import com.sojpt.model.bo.LkyScoreTable;
import com.sojpt.util.FileUtils;
import com.sojpt.util.WordUtils;
import com.sojpt.util.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Description: 本类用于
 * Created by SMW on 2020/2/11 17:55
 */
@RestController
@RequestMapping("/admin/major")
public class LkyOnlineReadingController {


    @Autowired
    private ResourceService resourceService;

    @Autowired
    private LkyScoreTableMapper lkyScoreTableMapper;


    @Value("${file.exportFolder}")
    private String exportFolder;

    @Value("${file.wordPath}")
    private String wordPath;

    /**
     *各种文件在线预览
     */
    @GetMapping("/toHtml")
    public void toHtml(HttpServletResponse response,String fileName) throws Throwable {
        resourceService.toHtml(response,fileName);
    }


    @PostMapping("/exportScore")
    public String export(HttpServletResponse response, String[] ids){

        // 2. sql查询  ，返回List<>
        List<LkyScoreTable> lkyScoreList = lkyScoreTableMapper.getLkyScoreTableByIds(ids);
        int counter = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        String dir = exportFolder + sdf.format(new Date()) + "/scoreFile";
        String dir1 = exportFolder + sdf.format(new Date());
        for (LkyScoreTable lkyScoreTable : lkyScoreList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", ++counter);
            map.put("name", lkyScoreTable.getName());
            map.put("college", lkyScoreTable.getCollege());
            map.put("major", lkyScoreTable.getMajor());
            map.put("className", lkyScoreTable.getClassName());
            map.put("list", lkyScoreTable.getEvaluationList());
            // 导出到本地
            try {
                WordUtils.exportWord(wordPath, dir,  map.get("name") +"_成绩表.docx", map,response);
            } catch (Exception e) {
                e.printStackTrace();
                return "0";
            }
        }
        return ZipUtils.saveZip(dir1, "学生成绩记录", response);
    }


    @GetMapping("/downloadZip")
    public void downloadZip(HttpServletResponse response, String fileName){
        try {
            boolean isDownload = FileUtils.downLoad(response, fileName);
            if (isDownload) {
                int i1 = fileName.lastIndexOf("/");
                String path = fileName.substring(0, i1);
                System.out.println("path = " + path);
                FileUtils.delAllFile(new File(path));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }


}
